﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility; 

namespace Runda.CMS.Portal.ContractManage
{
    public partial class ContractDocument : BasePage
    {
        public Guid ContractID
        {
            get
            {
                if (Request.QueryString["ContractID"] != null)
                {
                    return new Guid(Request.QueryString["ContractID"]);
                }
                return Guid.Empty;
            } 
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ContractID"] != null)
                { 
                    BindGrid();

                    CtrtContractEntity entity = new CtrtContractBll().Get<CtrtContractEntity>(ContractID);
                    if (entity != null)
                    {
                        if(entity.StartDate != DateTime.MinValue)
                        raddateStartDate.SelectedDate = entity.StartDate;
                        if (entity.EndDate != DateTime.MinValue)
                        raddateEndDate.SelectedDate = entity.EndDate;
                        if (entity.ContractEstimateSignDate != DateTime.MinValue)
                            txtContractEstimateSignDate.SelectedDate = entity.ContractEstimateSignDate;
                    }
                }
            }
        }

        private void BindGrid()
        {
            if(ContractID ==  Guid.Empty)return;
            List<CtrtDocumentEntity> list = new CtrtDocumentBll().GetAll<CtrtDocumentEntity>(" and ContractID = '"+ContractID+"'");
            RepeaterDocuments.DataSource = list;
            RepeaterDocuments.DataBind();
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            UploadFileBaseInfo uploadInfo = this.fuFilePath.UploadFile(Server.MapPath("../Uploads"));
            if (uploadInfo.IsSuccess)
            {
                CtrtDocumentEntity entity = new CtrtDocumentEntity();
                entity.ContractID = ContractID;
                entity.DocumentName = txtDocumentName.Text;
                entity.FileName = uploadInfo.FileName;
                entity.FileOriginalName = uploadInfo.FileOriginalName;
                entity.FilePath = uploadInfo.FilePath;
                entity.CreatedBy = base.CurrentUserID;
                entity.CreatedName = base.CurrentUserEntity.UserName;
                entity.CreatedOn = DateTime.Now;

                CtrtDocumentBll bll = new CtrtDocumentBll();
                bll.Insert(entity);
                BindGrid();
                    ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('上传成功');", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('" + uploadInfo.Message + "')", true);
            }
        }

        protected void RepeaterDocuments_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "download")
            {
                WebUtility.DownloadFile( e.CommandArgument.ToString().TrimStart('\\'),"","");
            }

            if (e.CommandName.ToLower() == "delete")
            {
                CtrtDocumentBll bll = new CtrtDocumentBll();
                //CtrtDocumentEntity documentEntity = bll.Get<CtrtDocumentEntity>(new Guid(e.CommandArgument.ToString()));
                CtrtDocumentEntity documentEntity =new CtrtDocumentEntity(new Guid(e.CommandArgument.ToString()));
                if (documentEntity != null)
                {
                    bll.Delete(documentEntity);
                }

                BindGrid();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            CtrtContractBll bll = new CtrtContractBll();
            CtrtContractEntity entity = bll.Get<CtrtContractEntity>(ContractID);
            if (entity != null)
            {
                if (raddateStartDate.SelectedDate .HasValue)
                    entity.StartDate = raddateStartDate.SelectedDate.Value;
                if (raddateEndDate.SelectedDate.HasValue)
                    entity.EndDate = raddateEndDate.SelectedDate.Value;
                if (txtContractEstimateSignDate.SelectedDate.HasValue)
                    entity.ContractEstimateSignDate = txtContractEstimateSignDate.SelectedDate.Value;
                bll.Update(entity);
            }
        }

       
    }
}
